import { createRouter, createWebHashHistory } from 'vue-router';
import { store } from '@/store';

/**
 * 所有的组件
 */
const files = require.context('@/views/', true, /\.vue$/);
/**
 * 路由
 */
const routes = [
  {
    path: '/',
    redirect: '/home'
  }
];

files.keys().forEach(key => {
  const paths = key.split('/');
  if (paths.length > 3) return;   //如果不是一级目录则跳过
  const name = paths[1];  //文件夹名称
  const component = files(key).default || files(key);
  routes.push({
    name: component.name || name,
    path: component.path,
    meta: component.meta || {},
    component: component.async ? component : () => import('@/views' + key.slice(1))  //判断是否需要同步加载
  });
});

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
});

/**
 * 路由守卫
 */
router.beforeEach((to, from, next) => {
  next(); //其他正常返回就行
});

export default router;
